/*****************************************************************************************
 * Aware : an Asynchonous Aware-Oriented Software Architecture
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *****************************************************************************************/
package dexter.aware;

import java.util.Hashtable;

import dexter.aware.db.DatabaseManager;
import dexter.core.host.HostAddress;
import dexter.util.Configuration;

/**
 * Monitor entity class
 * 
 * @author Ruben Mondejar <rmondejara@uoc.edu>
 */
public class Monitor { //extends Thread {

	//private static final int PORT = Configuration.getFrontPort();
	private static boolean init = false;
    
    public static synchronized void init() {
    	if (!init) {
    		initDatabase(false);
    		init = true;
    	}
    }
    
    private static void initDatabase(boolean create) {  
    	String driver = Configuration.getDatabaseDriver();
    	String url = Configuration.getDatabaseURL();
    	String user = Configuration.getDatabaseUser();
    	String pwd = Configuration.getDatabasePassword();
    	try {
			DatabaseManager.initDatabase(driver, url, user, pwd, create);
		} catch (Exception e) {		
			e.printStackTrace();
		}
    }

    
    public static void persist(HostAddress host, int[] state) {
    	init();
    	DatabaseManager.persistState(host.getAddress().getHostName(), state, System.currentTimeMillis());		
    }
    
	public static Hashtable<String, int[]> getPool() {
		init();
		Hashtable<String,int[]> hosts = DatabaseManager.findAllStates();
		return hosts;
	}
	
	public static Hashtable<String, int[]> getPool(int hours) {
		init();
		long millis = System.currentTimeMillis() - hours*60*60*1000; 
		java.sql.Timestamp top = new java.sql.Timestamp(millis);
		Hashtable<String,int[]> hosts = DatabaseManager.findAllStatesByTime(top);
		return hosts;
	}
	
	public static void mockDataCollection(int max) {
      
		for (int i=0; i<max; i++) {
			int cpu = (int) (Math.random()*100);
			int mem = (int) (Math.random()*100);
			int disk = (int) (Math.random()*100);
			DatabaseManager.persistState("planelab"+i+".upc.es",new int[]{cpu,mem,disk},System.currentTimeMillis()+(i*1000));	
		}				
	}

//	public static void main(String[] args) throws Exception{
//    	Monitor monitor = new Monitor(PORT);
//		//monitor.start();
//    	
//    	//mockDataCollection(44);
//    	System.in.read();
//    	
//    }




    
}   
